<template>
  <el-container class="home-container">
    <el-header>
      <div>
        <img src="../../assets/logo.png"/>
        <span>后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>

    <el-container>
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleClapse">{{ optionInfo }}</div>
        <el-menu
            background-color="#333744"
            text-color="#fff"
            active-text-color="#ffd04b"
            router
            unique-opened
            :collapse="isCollapse"
            :collapse-transition="false">

          <el-submenu :index="item.code + ''" v-for="item in menuList" :key="item.code">

            <template slot="title">
              <i class="el-icon-s-home"></i>
              <span>{{ item.name }}</span>
            </template>

            <el-menu-item :index="subMenu.url+''" v-for="subMenu in item.children" :key="subMenu.code">

              <template slot="title">
                <i class="el-icon-setting"></i>
                <span>{{ subMenu.name }}</span>
              </template>

            </el-menu-item>

          </el-submenu>

        </el-menu>
      </el-aside>

      <el-main>
        <!-- 子路由填充位 -->
        <router-view/>

      </el-main>
    </el-container>

  </el-container>
</template>

<script>
/* {
   id: 1,
   name: "用户管理",
   children: [
     {id: 11, name: "用户列表", url: "/system/user"},
     {id: 12, name: "业主列表", url: "/system/"},
   ]
 },*/
export default {
  data() {
    return {
      menuList: [],
      isCollapse: false,
      isRouterAlive: true,
      optionInfo: 'close'
    };
  },
  methods: {
    reload() {
      this.isRouterAlive = false;
      this.$nextTick(function () {
        this.isRouterAlive = true;
      });
    },
    toggleClapse() {
      this.isCollapse = !this.isCollapse;
      if (this.optionInfo == 'close') {
        this.optionInfo = 'open'
      } else {
        this.optionInfo = 'close'
      }
    },
    logout() {
      window.sessionStorage.clear();
      this.$router.push("/");
    },
    loadMenus() {
      let userName = sessionStorage.getItem("loginName");
      let url = "/permission/getMenuByName";
      this.$axios.get(url,{
        params: {
          userName: userName
        }
      }).then(res => {
        if (res.data.code == 200) {
          // 把我们查到的菜单数据，给到 menuList
          this.menuList = res.data.data;
        } else {
          this.$message({
            type: "error",
            message: "菜单加载错误",
            duration: 1000
          })
        }
      })

    }
  },
  mounted() {
    // this.loadMenus();
  }
};
</script>

<style lang="less" scoped>

img {
  width: 30px;
  height: 30px;
}

.toggle-button {
  padding: 5px 0;
  background-color: #373d48;
  font-size: 10px;
  text-align: center;
  color: #fff;
  letter-spacing: 0.2em;
  cursor: pointer;
}

.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 22px;

  > div {
    display: flex;
    align-items: center;

    span {
      margin-left: 14px;
    }
  }
}

.el-aside {
  background-color: #333744;

  .el-menu {
    border-right: 0;
  }
}

.el-main {
  background-color: #eaedf1;
}

.home-container {
  height: 100%;
}

</style>
